package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.constant.MessageConstant;
import com.atguigu.constant.RedisConstant;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.entity.Result;
import com.atguigu.pojo.Setmeal;
import com.atguigu.service.SetmealService;
import com.atguigu.util.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Reference
    SetmealService setmealService;

    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile) throws IOException {
        String filename = imgFile.getOriginalFilename();
        String suffixName = filename.substring(filename.lastIndexOf("."));
        filename= UUID.randomUUID().toString()+System.currentTimeMillis()+"-"+suffixName;
        //上传图片到七牛云
        QiniuUtils.upload2Qiniu(imgFile.getBytes(),filename);
        jedisPool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES,filename);
        return   new Result(true, MessageConstant.UPLOAD_SUCCESS,filename);
    }



    @RequestMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] travelgroupIds){
        Result result = setmealService.add(setmeal,travelgroupIds);
        return result;

    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

}
